<?php
namespace Admin\Controller;
use Think\Controller;

/**
* 系统设置
*/
class SystemController extends BaseController
{
	private $last_time=null;
	public function editPwd()
	{
		if(IS_POST)
		{
			$data = $_POST;
			$user = M('admin')->where("username = '".session('username')."'")->find();
			if($user['password'] != $data['oldpwd'])
			{
				returnJsonStr(1, '旧密码不正确！');
			}

			$res['password'] = $data['password'];
			$res['edit_time'] = getMillisecond();
			$ret = M('admin')->where('id = '.$user['id'])->save($res);
			if($ret)
			{
				returnJsonStr(0, '密码修改成功');	
			}
			else
			{
				returnJsonStr(1, '密码修改失败');
			}
		}
		$this->display();
	}

	public function setSignStatus()
	{
		$aid = session('admin_id');
		if(IS_POST)
		{
			$msg = '';
			$data = $_POST;
			$data['update_time'] = getMillisecond();
			$data['aid'] = $aid;
			$res = M('sign_status')->add($data);
			if($res)
			{
				if($data['status'] == 1)
				{
					$msg = '报名活动开启成功！';
				}
				else if($data['status'] == 0)
				{
					$msg = '报名活动关闭成功！';
				}
				returnJsonStr(0, $msg);
			}
			else
			{
				returnJsonStr(1, '设置报名活动状态失败！');
			}
		}

		$info = M('sign_status')->where('aid = '.$aid)->field('status')->order('update_time desc')->find();
		$this->assign('status', $info['status']);
		$this->display();
	}

	// 参会二维码界面
	public function createQRcode()
	{
		$this->display();
	}

	// 生成参会二维码
	public function getToken()
	{
		$width = empty($_POST['width']) ? 430 : $_POST['width'];
		$uri = "https://api.weixin.qq.com/cgi-bin/token?";

		$params = array(
			"grant_type" => 'client_credential',
			"appid"		 =>	C('XCX_APPID'),
			'secret'	 => C('XCX_SECRET')	
		);

		$param = http_build_query($params);

		$url = $uri . $param;

		$ret = cUrl_get($url);
		$request_url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$ret['access_token'];  // B
		// $request_url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=".$ret['access_token'];  // A
		// $request_url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$ret['access_token'];  // C
		
		$num = 1;
		$data = array(
			'scene'	=> 'meeting_num='.$num,
			'page'  => 'pages/join/join',
			"width" => $width,
			'auto_color' => false,
			"line_color" =>  array("r"=>0, 'g'=>0, 'b'=> 0), //'{"r":"100","g":"125","b":"25"}',
			"is_hyaline" => false
		);
		
		$time = time();
		// $result = cUrl_post_json(json_encode($data), $request_url);
		$result = http_post($request_url, json_encode($data));
		$file = "/Public/qrcode/smallwechat".$time.".png";
		file_put_contents('.'.$file, $result);
		$sqlData = array(
			'path'	=> $file,
			'size'	=> $width,
			'time'	=> getMillisecond()
		);
		$m_res = M('qrcode')->add($sqlData);

		echo $file;
	}


	public function clearAll()
	{
		$sql = "
			INSERT INTO jy_userinfo_history 
				(`uid`, `openid`, `username`, `phone`, `ident`, `school`, `province`, `city`, `county`, `province_name`, `city_name`, `county_name`, `address`, `grade`, `class`, `sum`, `reg_time`, `join_time`, `del_time`)
			SELECT *, unix_timestamp(now()) FROM jy_userinfo
		";
		$res = M()->execute($sql);
		if($res)
		{
			$del_sql = "DELETE FROM jy_userinfo";
			$ret = M()->execute($del_sql);
			if($ret)
			{
				returnJsonStr(0, '清空数据完成！');
			}
			else
			{
				returnJsonStr(1, '清空数据失败！');
			}
		}
		else
		{
			returnJsonStr(1, '清空数据失败！可能没有用户数据！');
		}

	}

}


